package user.manage;

import groupby.model.Category;
import groupby.model.Groupby;
import groupby.persistence.GroupbyDao;
import groupby.util.Pages;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import system.model.BasicInfor;
import system.model.SystemInfor;
import user.model.BrandInfor;
import user.model.Dynamic;
import user.model.Photoinfor;
import user.model.Pictures;
import user.model.Responses;
import user.model.Topic;
import user.model.UserDetial;
import user.model.Users;
import user.persistence.UsersDao;

public class UsersManageImpl implements UsersManage {

	private UsersDao userDao=null;
	private GroupbyDao groupDao = null;
	public GroupbyDao getGroupDao() {
		return groupDao;
	}
	public void setGroupDao(GroupbyDao groupDao) {
		this.groupDao = groupDao;
	}
	public UsersDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UsersDao userDao) {
		this.userDao = userDao;
	}
	/**
	 * 用户登录
	 */
	public Users usersLogin(String email,String password) {
		Users user=userDao.usersLogin(email,password);
		return user;
	}
	/**
	 * 用户列表
	 */
	public JSONObject getUserList(Pages pages,String email,String startTime,String endTime,int levels ,int able) {
		List userList=userDao.getUserList(pages,email,startTime,endTime,levels,able);
		JSONObject jsonObject=new JSONObject();
		JSONArray josnArray=new JSONArray();
		for(int i=0;i<userList.size();i++)
		{
			Users user=(Users)userList.get(i);
			JSONObject tempObject=new JSONObject();
			tempObject.put("email", user.getEmail());
			String level="";
			if(user.getLevel()==2)
			{
				level="普通用户";
			}else if(user.getLevel()==3)
			{
				level="团购用户";
			}else if(user.getLevel()==4)
			{
				level="品牌用户";
			}
			else if(user.getLevel()==5)
			{
				level="网站管理员";
			}
			else if(user.getLevel()==6)
			{
				level="网站总编";
			}
			else if(user.getLevel()==7)
			{
				level="网站责任编辑";
			}else if(user.getLevel()==8)
			{
				level="网站采编";
			}
		
			tempObject.put("level", level);
			UserDetial userDetial=userDao.getSingleUser(user.getId());
			tempObject.put("tel", userDetial.getTel()==null?" ":userDetial.getTel());
			tempObject.put("trueName", userDetial.getTrueName()==null?" ":userDetial.getTrueName());
			String isAble="";
			if(user.getAble()==1)
			{
				isAble="<a href='javascript:void(0)' onclick='ratify("+user.getId()+",0,this);' id='u"+user.getId()+"'><font color='blue'>开通</color></a>";
			}else
			{
				isAble="<a href='javascript:ratify("+user.getId()+",1);' id='u"+user.getId()+"'><font color='red'>未开通</color></a>";
			}
			
			tempObject.put("registerTime",user.getRegisterTime());
			tempObject.put("able", isAble);
			tempObject.put("del","<a href='javascript:delUser("+user.getId()+")' >删除</a>");
			josnArray.add(tempObject);
		}
		jsonObject.put("total", pages.getRowCount());
		jsonObject.put("rows", josnArray);
		System.out.println(jsonObject.toString());
		return jsonObject;
	}
	/**
	 * 开通申请的用户
	 */
	public JSONObject ratify(int id, int able) {
		boolean isSuccess=userDao.ratify(id,able);
		JSONObject josnObject=new JSONObject();
		    josnObject.put("success", isSuccess);
		return josnObject;
	}
	/**
	 * 删除用户
	 */
	public JSONObject deleteUser(int id) {
		 boolean isSuccess=userDao.deleteUser(id);
		 JSONObject josnObject=new JSONObject();
		    josnObject.put("success", isSuccess);
		return josnObject;
	}
	/**
	 * 获得用户信息
	 */
	public UserDetial getSingleUser(int id) {
		System.out.println(id);
		UserDetial userDetial=userDao.getSingleUser(id);
		return userDetial;
	}
	/**
	 * 获得用户信息
	 */
	public UserDetial getSingleUser(String userName) {
		UserDetial userDetial=userDao.getSingleUser(userName);
		
		return userDetial;
	}
	/**
	 * 更新用户信息
	 */
	public void updateSingleUser(UserDetial userDetial) {
		userDao.updateSingleUser(userDetial);
	}
	public List getLikeGroupby(String tags,int city) {
		List list=groupDao.getLikeGroupby(tags,city);
		
		return list;
	}
	/**
	 * checkUserEmail
	 */
	public JSONObject checkUserEmail(String email) {
		boolean isSuccess=userDao.checkUserEmail(email);
		JSONObject jsonObject=new JSONObject();
		           jsonObject.put("success", isSuccess);
		return jsonObject;
	}
	/**
	 * 添加用户
	 */
	public int addUser(Users user) {
		int uid=userDao.addUser(user);
		return uid;
	}
	/**
	 * 添加用户细节
	 */
	public boolean addUserDetial(UserDetial userDetial) {
		 boolean isSuccess=userDao.addUserDetial(userDetial);
		return isSuccess;
	}
	/**
	 * 获得Content
	 */
	public BasicInfor getBasicInfor(int categoryId) {
		
		return userDao.getBasicInfor(categoryId);
	}
	/**
	 * 修改BasicInfor
	 */
	public boolean updateBasicInfor(BasicInfor bi) {
		
		return userDao.updateBasicInfor(bi);
	}
	/**
	 * 获得团够列表
	 */
	public List attentionGroupbyList(Integer userId) {
		List list=userDao.attentionGroupbyList(userId);
		return list;
	}
	/**
	 * 修改密码
	 */
	public JSONObject modifyUserPassword(Users users) {
		
		boolean isSuccess=userDao.modifyUserPassword(users);
		JSONObject  jsonObject=new JSONObject();
		           jsonObject.put("success", isSuccess);
		return jsonObject;
	}
	/**
	 * 获得我的好友
	 */
	public List myFriendList(Pages pages,Integer id,int level) {
		  List list=userDao.myFriendList(pages,id,level);
		return list;
	}
    /**
     * 添加好友
     */
	public JSONObject addFriend(int userId,int id) {
		boolean isSuccess=userDao.addFriend(userId,id);
		JSONObject  jsonObject=new JSONObject();
        jsonObject.put("success", isSuccess);
		return jsonObject;
	}
	/**
	 * 获得用户
	 */
	public Users getUser(int id) {
		Users users=userDao.getUsers(id);
		return users;
	}
	/**
	 * 添加动态
	 */
	public void addDynamic(Dynamic dynamic) {
		userDao.addDynamic(dynamic);
	}

	
	/**
	 * 获得动态信息
	 */
	@SuppressWarnings("unchecked")
	public List dynamicList(Pages page,int id) {
		List list =userDao.dynamicList(page,id);
		List tempList=new ArrayList();
		if(list!=null)
		{
		for(int i=0;i<list.size();i++)
		{
			try
			{
			List temp=new ArrayList();
			Dynamic dynamic=(Dynamic)list.get(i);
			dynamic.setUser2(dynamic.getUser2().substring(0,dynamic.getUser2().length()-1));
			String[] tempArray=new String[11];
			List listReplay=null;
			if(dynamic.getFlag()==1)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				UserDetial user2=userDao.getSingleUser(Integer.parseInt(dynamic.getUser2().split("<>")[1].split(",")[0]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
					
				}
				if(user2.getImage()==null||user2.getImage().equals(""))
				{
					
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					
					tempArray[4]="uploadFile/"+user2.getImage();
				}
				
				
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=user2.getUserId().toString();
				if(user2.getUsername()==null||user2.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user2.getUserId());
					tempArray[5]=user.getEmail();
				}else
				{
					tempArray[5]=user2.getUsername();
				}
				tempArray[6]="";
				tempArray[7]=dynamic.getFlag().toString();
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				      user=userDao.getUsers(user2.getUserId());
				tempArray[10]=user.getLevel().toString();
			}else if(dynamic.getFlag()==2)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				UserDetial user2=userDao.getSingleUser(Integer.parseInt(dynamic.getUser2().split("<>")[1].split(",")[0]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
					
				}
				if(user2.getImage()==null||user2.getImage().equals(""))
				{
					
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					
					tempArray[4]="uploadFile/"+user2.getImage();
				}
				
				
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=user2.getUserId().toString();
				if(user2.getUsername()==null||user2.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user2.getUserId());
					tempArray[5]=user.getEmail();
				}else
				{
					tempArray[5]=user2.getUsername();
				}
				tempArray[6]="";
				tempArray[7]=dynamic.getFlag().toString();
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				      user=userDao.getUsers(user2.getUserId());
				tempArray[10]=user.getLevel().toString();
			}else if(dynamic.getFlag()==3)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				UserDetial user2=userDao.getSingleUser(Integer.parseInt(dynamic.getUser2().split("<>")[1].split(",")[0]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
					
				}
				if(user2.getImage()==null||user2.getImage().equals(""))
				{
					
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					
					tempArray[4]="uploadFile/"+user2.getImage();
				}
				
				
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=user2.getUserId().toString();
				if(user2.getUsername()==null||user2.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user2.getUserId());
					tempArray[5]=user.getEmail();
				}else
				{
					tempArray[5]=user2.getUsername();
				}
				tempArray[6]=dynamic.getContent();
				tempArray[7]=dynamic.getFlag().toString();
				listReplay=userDao.listReplay(dynamic.getId());
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				      user=userDao.getUsers(user2.getUserId());
				tempArray[10]=user.getLevel().toString();
			}else if(dynamic.getFlag()==4)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				Groupby groupby=groupDao.criticismGroupbyId(Integer.parseInt(dynamic.getUser2().split("<>")[1].split(",")[0]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
					
				}
				if(groupby.getImage()==null||groupby.getImage().equals(""))
				{
					
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					
					tempArray[4]=groupby.getImage();
				}
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=groupby.getId().toString();
				tempArray[5]=groupby.getWebsite()+","+groupby.getDealUrl();;
				tempArray[6]="";
				tempArray[7]=dynamic.getFlag().toString();
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				     
				tempArray[10]="-1";
			}else if(dynamic.getFlag()==5)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				Groupby groupby=groupDao.criticismGroupbyId(Integer.parseInt(dynamic.getUser2().split("<>")[1].split(",")[0]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
				}
				if(groupby.getImage()==null||groupby.getImage().equals(""))
				{
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					tempArray[4]=groupby.getImage();
				}
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=groupby.getId().toString();
				
				tempArray[5]=groupby.getWebsite()+","+groupby.getDealUrl();
				tempArray[6]=dynamic.getContent();
				tempArray[7]=dynamic.getFlag().toString();
				listReplay=userDao.listReplay(dynamic.getId());
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				     
				tempArray[10]="-1";
			}else if(dynamic.getFlag()==6)
			{
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				UserDetial user2=userDao.getSingleUser(Integer.parseInt(dynamic.getUser2().split("<>")[1]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
					
				}
				if(user2.getImage()==null||user2.getImage().equals(""))
				{
					
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					
					tempArray[4]="uploadFile/"+user2.getImage();
				}
				tempArray[2]=user1.getUsername();
				tempArray[3]=user2.getUserId().toString();
				if(user2.getUsername()==null||user2.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user2.getUserId());
					tempArray[5]=user.getEmail();
				}else
				{
					tempArray[5]=user2.getUsername();
				}
				tempArray[6]="";
				tempArray[7]=dynamic.getFlag().toString();
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				      user=userDao.getUsers(user2.getUserId());
				tempArray[10]=user.getLevel().toString();
			}else if(dynamic.getFlag()==7)
			{
				
				UserDetial user1=userDao.getSingleUser(Integer.parseInt(dynamic.getUser1().split("<>")[1]));
				UserDetial user2=userDao.getSingleUser(Integer.parseInt(dynamic.getUser2().split("<>")[1]));
				tempArray[0]=user1.getUserId().toString();
				if(user1.getImage()==null||user1.getImage().equals(""))
				{
					tempArray[1]="uploadFile/img.gif";
					
				}else
				{
					tempArray[1]="uploadFile/"+user1.getImage();
				}
				if(user2.getImage()==null||user2.getImage().equals(""))
				{
					tempArray[4]="uploadFile/img.gif";
				}else
				{
					tempArray[4]=user2.getImage();
				}
				if(user1.getUsername()==null||user1.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user1.getUserId());
					tempArray[2]=user.getEmail();
				}else
				{
					tempArray[2]=user1.getUsername();
				}
				tempArray[3]=user2.getUserId().toString();
				
				if(user2.getUsername()==null||user2.getUsername().equals(""))
				{
					Users user = userDao.getUsers(user2.getUserId());
					tempArray[5]=user.getEmail();
				}else
				{
					tempArray[5]=user2.getUsername();
				}
				tempArray[6]=dynamic.getContent();
				tempArray[7]=dynamic.getFlag().toString();
				listReplay=userDao.listReplay(dynamic.getId());
				tempArray[8]=dynamic.getId().toString();
				Users user=userDao.getUsers(user1.getUserId());
				tempArray[9]=user.getLevel().toString();
				      user=userDao.getUsers(user2.getUserId());
				tempArray[10]=user.getLevel().toString();
			}
			temp.add(tempArray);
			temp.add(listReplay);
			
			tempList.add(temp);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		}
		return tempList;
	}
	/**
	 * 添加回应
	 */
	public JSONObject addReplay(Responses responses) {
		boolean isSuccess=userDao.addReplay(responses);
		JSONObject json=new JSONObject();
		json.put("success",responses );
		return json;
	}
	/**
	 * 获得评论信息
	 */
	public List myCriticism(Pages pages, Integer id) {
		List list=userDao.myCriticism(pages,id);
		 
			return list;
		
	}
	/**
	 * 添加相册
	 */
	public boolean addPictures(Pictures picture) {
	     
		return userDao.addPictures(picture);
	}
	/**
	 * 获得相册列表
	 */
	public JSONObject pictureList(Pages pages,String photoName,String startDate,String endDate,Integer id,int categoryId,String flag) {
		List userList=userDao.pictureList(pages,photoName,startDate,endDate,id,"",categoryId,flag);
		JSONObject jsonObject=new JSONObject();
		JSONArray josnArray=new JSONArray();
		for(int i=0;i<userList.size();i++)
		{
			Pictures pic=(Pictures)userList.get(i);
			JSONObject tempObject=new JSONObject();
			tempObject.put("name","<a href='branduser!photoList.action?currentPage=1&picId="+pic.getId()+"'>"+pic.getPhotoName()+"</a>" );
			tempObject.put("description",pic.getDescription());
		
			UserDetial userDetial=userDao.getSingleUser(pic.getUserId());
			if(userDetial==null)
			{
				continue;
			}
			if(userDetial.getUsername()!=null&&!userDetial.getUsername().equals(""))
			{
				tempObject.put("brand",userDetial.getUsername());
			}else
			{
				tempObject.put("brand",userDetial.getEusername());
			}
			Category category=groupDao.findCategoryById(pic.getCategoryId());
			
				tempObject.put("category",category.getCategoryName());
			
			String isAble="";
			if(pic.getRecommend()==1)
			{
				isAble="<a href='javascript:void(0)' onclick='ratifyPic("+pic.getId()+",0);' id='u"+pic.getId()+"'><font color='blue'>推荐</color></a>";
			}else
			{
				isAble="<a href='javascript:ratifyPic("+pic.getId()+",1);' id='u"+pic.getId()+"'><font color='red'>未推荐</color></a>";
			}
			
			tempObject.put("datetime",pic.getDatetime());
			
			tempObject.put("status",isAble);
			tempObject.put("modify","<a href='javascript:void(0);' onclick='modify("+pic.getId()+",this)' >修改</a>");
			tempObject.put("del","<a href='javascript:delPictures("+pic.getId()+")' >删除</a>");
			josnArray.add(tempObject);
		}
		jsonObject.put("total", pages.getRowCount());
		jsonObject.put("rows", josnArray);
		System.out.println(jsonObject.toString());
		return jsonObject;
	}
	/**
	 * 删除相册
	 */
	public JSONObject delPictures(int picId,String path) {
		boolean isSuccess=userDao.delPictures(picId,path);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 推荐相册
	 */
	public JSONObject ratifyPic(int picId, int able) {
		boolean isSuccess=userDao.ratifyPic(picId,able);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 获得相册列表
	 */
	@SuppressWarnings("unchecked")
	public List picturesList(Pages pages, Integer id,String path,String flag) {
		List userList=userDao.pictureList(pages,"","","",id,flag,-1,"");
		List tempLlist=new ArrayList();
		for(int i=0;i<userList.size();i++)
		{
			Pictures pic=(Pictures)userList.get(i);
			String[] tempArray=new String[9];
			tempArray[0]=pic.getUserId().toString();
			tempArray[1]=pic.getPhotoName();
			tempArray[2]=pic.getDescription();
			tempArray[3]=pic.getImageFolder();
			tempArray[4]=pic.getFace();
			tempArray[5]=pic.getDatetime();
			tempArray[6]=pic.getRecommend().toString();
			int size=0;
			try
			{
			path=path+"\\"+pic.getImageFolder();
			path=path.replace("/", File.separator).replace("\\",File.separator);
			File file=new File(path);
			 size=file.list().length;
			}catch(Exception e)
			{}
			tempArray[7]=size+"";
			tempArray[8]=pic.getId().toString();
			tempLlist.add(tempArray);
		}
		return tempLlist;
	}
	/**
	 * 获得相册列表for option
	 */
	public JSONObject picturesList(Integer id) {
		List userList=userDao.pictureList(null,"","","",id,"yes",-1,"");
		
		JSONObject json=new JSONObject();
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<userList.size();i++)
		{
			Pictures pic=(Pictures)userList.get(i);
			sb.append("<option value='"+pic.getId()+"'>"+pic.getPhotoName()+"</option>");
		}
	
		json.put("select", sb.toString());
		return json;
	}
	/**
	 * 获得单个相册
	 */
	public Pictures getPictures(int picId) {
		
		return userDao.getPictures(picId);
	}
	/**
	 * 添加图片
	 */
	public boolean addPhoto(Photoinfor pi) {
		boolean isSuccess=userDao.addPhoto(pi);
		return isSuccess;
	}
	/**
	 * 显示相册图片
	 */
	public List showPhotoList(Pages pages, int picId) {
	    List list=userDao.showPhotoList(pages,picId);
		return list;
	}
	/**
	 * 回应相册图片
	 */
	public Photoinfor criticismPhoto(Pages pages, int photoId,int picId) {
		Photoinfor photoinfor=userDao.criticismPhoto(pages,photoId,picId);
			return photoinfor;
	}
	/**
	 * 添加品牌
	 */
	public JSONObject addBrandInfor(BrandInfor brandInfor) {
		boolean isSuccess=userDao.addBrandInfor(brandInfor);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 获得品牌列表信息
	 */
	public List brandList(Pages picPages, int id) {
		List list=userDao.brandList(picPages, id);
		return list;
	}
	/**
	 * 获得品牌列表
	 */
	public List brandInforAllList(Pages pages, Integer id) {
		List list=userDao.brandInforAllList(pages,id);
		return list;
	}
	/**
	 * 获得单个类别品牌列表
	 */
	@SuppressWarnings("unchecked")
	public List brandSingleCategoryList(Pages pages, Integer id, int category) {
		List list=userDao.brandSingleCategoryList(pages,id,category);
		return list;
	}
	/**
	 * 获得单个品牌信息
	 */
	public BrandInfor singleBrand(int brandId) {
		
		return userDao.singleBrand(brandId);
	}
	/**
	 * 添加话题
	 */
	public boolean addTopic(Topic topic) {
		
		return userDao.addTopic(topic);
	}
	/**
	 * 获得话题列表
	 */
	public List topicList(Pages pages,Integer id) {
		List list=userDao.topicList(pages,id,"","","");
		
		return list;
		
	}
	/**
	 * 获得单个话题
	 */
	public Topic getTopic(int topicId) {
		
		return userDao.getTopic(topicId);
	}
	/**
	 * 更新TopicCount
	 */
	public void updateTopicCount(int topicId) {
		 userDao.updateTopicCount(topicId);
		
	}
	/**
	 * 获得其它列表
	 */
	public List topicList(Pages pages, int userId, int topicId) {
		return userDao.topicList(pages,userId,topicId);
	}
	public JSONObject manageBrandInforAllList(Pages pages, Integer id,String title,String startDate,String endDate,int categoryId,String flag) {
		List list=userDao.brandInforAllList(pages,id,title,startDate,endDate,categoryId,flag);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			
			JSONObject jsonTemp=new JSONObject();
			BrandInfor bi=(BrandInfor)((Object[])list.get(i))[0];
			Category category=(Category)((Object[])list.get(i))[1];
			jsonTemp.put("bid",bi.getId());
			jsonTemp.put("cid",category.getId());
			jsonTemp.put("title","<a href='branduser!singleBrandInfor.action?brandId="+bi.getId()+"&flag=0'>"+bi.getTitle()+"</a>");
			jsonTemp.put("category",category.getCategoryName());
			jsonTemp.put("datetime",bi.getDatetime());
			
			UserDetial userDetial=userDao.getSingleUser(Integer.parseInt(bi.getUserId()));
			if(userDetial==null)
			{
				continue;
			}
			if(userDetial.getUsername()!=null&&!userDetial.getUsername().equals(""))
			{
				jsonTemp.put("brand",userDetial.getUsername());
			}else
			{
				jsonTemp.put("brand",userDetial.getEusername());
			}
			jsonTemp.put("modify","<a href='branduser!singleBrandInfor.action?brandId="+bi.getId()+"&flag=1'>修改</a>");
			jsonTemp.put("delete","<a href='javascript:del("+bi.getId()+");'>删除</a>");
			String isAble="";
			if(bi.getRecommendation()!=null&&bi.getRecommendation()==1)
			{
				isAble="<a href='javascript:void(0)' onclick='recommendation("+bi.getId()+",0);' id='u"+bi.getId()+"'><font color='blue'>推荐</color></a>";
			}else
			{
				isAble="<a href='javascript:void(0)' onclick='recommendation("+bi.getId()+",1);' id='u"+bi.getId()+"'><font color='red'>未推荐</color></a>";
			}
			jsonTemp.put("recommendation",isAble);
			String top="";
			if(bi.getTop()!=null&&bi.getTop()==1)
			{
				top="<a href='javascript:void(0)' onclick='top("+bi.getId()+",0);' id='tu"+bi.getId()+"'><font color='blue'>置顶</color></a>";
			}else
			{
				top="<a href='javascript:void(0)' onclick='top("+bi.getId()+",1);' id='tu"+bi.getId()+"'><font color='red'>未置顶</color></a>";
			}
			jsonTemp.put("top",top);
			jsonArray.add(jsonTemp);
		}
		json.put("total", pages.getRowCount());
		json.put("rows",jsonArray);
		return json;
	}
	/**
	 * 获得单个品牌信息
	 */
	public JSONObject getBrand(int brandId) {
		BrandInfor brandInfor=userDao.singleBrand(brandId);
		JSONObject  json=new JSONObject();
		json.put("brand",brandInfor);
		return json;
	}
	/**
	 * 删除品牌图片
	 */
	public JSONObject delImage(int brandId, String flag,String image,String path) {
		boolean isSuccess=userDao.delImage(brandId,flag, image,path);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 上传品牌单个图上
	 */
	public JSONObject uploadSingleImage(int brandId, String flag, String image) {
		boolean isSuccess=userDao.uploadSingleImage(brandId,flag, image);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 更新brand信息
	 */
	public JSONObject modifyBrand(int brandId, String title, String content,int category,int category2) {
		boolean isSuccess=userDao.modifyBrand(brandId,title,content,category,category2);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 推荐品牌
	 */
	public JSONObject modifyBrand(int brandId, String flag) {
		boolean isSuccess=userDao.modifyBrand(brandId,flag);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 获得品牌信息
	 */
	public List brandList(Pages page,boolean flag,String category) {
		List list=userDao.brandList(page,flag,category);
		return list;
	}
	/*
	 * 获得单个　类别的所有品牌信息
	 * 
	 */
	public List brandSingleCategoryList(Pages pages, Integer id,String flag) {
		List list=userDao.brandSingleCategoryList(pages,id,flag);
		return list;
	}
	/**
	 * 获得推荐用用户列表
	 */
	@SuppressWarnings("unchecked")
	public JSONObject userList(Pages pages, String email, String startTime,
			String endTime, String categoryId) {
		List userList=userDao.userList(pages,email,startTime,endTime,categoryId);
		JSONObject jsonObject=new JSONObject();
		JSONArray josnArray=new JSONArray();
		for(int i=0;i<userList.size();i++)
		{
			Users user=(Users)userList.get(i);
			JSONObject tempObject=new JSONObject();
			tempObject.put("email", user.getEmail());
			String level="";
			if(user.getLevel()==2)
			{
				level="普通用户";
			}else if(user.getLevel()==3)
			{
				level="团购用户";
			}else if(user.getLevel()==4)
			{
				level="品牌用户";
			}
			else if(user.getLevel()==5)
			{
				level="网站管理员";
			}
			else if(user.getLevel()==6)
			{
				level="网站总编";
			}
			else if(user.getLevel()==7)
			{
				level="网站责任编辑";
			}else if(user.getLevel()==8)
			{
				level="网站采编";
			}
		
			tempObject.put("level", level);
			String recommend="";
			if(user.getRecommend()==0)
			{
				recommend="<a href='javascript:void(0)' onclick='recommend("+user.getId()+",1,this);' id='u"+user.getId()+"'><font color='blue'>推荐</color></a>";
			}else
			{
				recommend="<a href='javascript:void(0)' onclick='recommend("+user.getId()+",0,this);' id='u"+user.getId()+"'><font color='red'>取消推荐</color></a>";
			}
			
			tempObject.put("registerTime",user.getRegisterTime());
			tempObject.put("recommend", recommend);
			
			josnArray.add(tempObject);
		}
		jsonObject.put("total", pages.getRowCount());
		jsonObject.put("rows", josnArray);
		
		return jsonObject;
	}
	/**
	 * 推荐用户
	 */
	public JSONObject recommendUser(int userId, int flag) {
		boolean isSuccess=userDao.recommendUser(userId,flag);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 获得推荐品牌用户
	 */
	public List brandUserList(Pages page, int level,int flag) {
		List userList=userDao.brandUserList(page,level,flag);
		return userList;
	}

	/**
	 * 获得品牌用户 by category
	 */
	public List brandUserListByCategory(Pages page, Integer id) {
		List list=userDao.brandUserListMore(page,id);
		return list;
	}
	/**
	 * 查找picture by category
	 */
	public List pictureListByCategory(Pages picPages, Integer id) {
		List list=userDao.pictureListByCategory(picPages,id);
		return list;
	}
	/**
	 * 修改相册图片信息　
	 */
	public JSONObject photoModify(Photoinfor photoinfor) {
		boolean isSuccess=userDao.photoModify(photoinfor);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 删除相册里的图片
	 */
	public JSONObject photoDel(int photoId,String path) {
		boolean isSuccess=userDao.photoDel(photoId,path);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 设图片相册封面
	 */
	public JSONObject pictureFace(int picId, String image) {
		boolean isSuccess=userDao.pictureFace(picId,image);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 获得关注我的人
	 */
	public List friendToMeList(Pages pages, int userId) {
		List list=userDao.friendToMeList(pages,userId);
		return list;
	}
	/**
	 * 删除品牌图片
	 */
	public JSONObject delBrand(int brandId, String path) {
		boolean isSuccess=userDao.delBrand(brandId,path);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 话题管理
	 */
	public JSONObject topicListForJson(Pages pages,int id, String title,
			String startDate, String endDate) {
		List list=userDao.topicList(pages,id,title,startDate,endDate);
		JSONObject jsonObject=new JSONObject();
		JSONArray josnArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Topic topic=(Topic)((Object[])list.get(i))[0];
			UserDetial userdetial=(UserDetial)((Object[])list.get(i))[1];
			JSONObject tempObject=new JSONObject();
			tempObject.put("title", topic.getTitle());
			tempObject.put("content", topic.getContent());
			tempObject.put("replay", topic.getReplyCount());
			tempObject.put("user",userdetial.getUsername());
			tempObject.put("lastTime", topic.getReplyLastTime());
			tempObject.put("modify", "<a href='javascript:void(0)' onclick='modify("+topic.getId()+",this);' ><font color='blue'>修改</color></a>");
			tempObject.put("del", "<a href='javascript:void(0)' onclick='del("+topic.getId()+");' ><font color='blue'>删除</color></a>");
			josnArray.add(tempObject);
		}
		jsonObject.put("total", pages.getRowCount());
		jsonObject.put("rows", josnArray);
		return jsonObject;
	}
	/**
	 * 话题删除
	 */
	public JSONObject delTopic(int topicId) {
		boolean isSuccess=userDao.delTopic(topicId);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 更新话题
	 */
	public JSONObject modifyTopic(Topic topicInfor) {
		boolean isSuccess=userDao.modifyTopic(topicInfor);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 修改邮箱
	 */
	public JSONObject modifyEmail(Users users) {
		boolean isSuccess=userDao.modifyEmail(users);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 修改相册
	 */
	public JSONObject modifyPicture(Pictures pic) {
		boolean isSuccess=userDao.modifyPicture(pic);
		JSONObject  json=new JSONObject();
		json.put("success",isSuccess);
		return json;
	}
	/**
	 * 找回密码
	 */
	public JSONObject findUserPassword(String email,SystemInfor systemInfor) {
		JSONObject json=new JSONObject();
		try
		{
		Users user=userDao.findUserPassword(email);
		 
           try{ 
		        	
		   			
		   			String host = "smtp.163.com";
		   			Authenticator auth = new PopupAuthenticator(systemInfor.getWebEmail(), systemInfor.getWebEmailPass());
		   			Properties mailProps = new Properties();
		   			mailProps.put("mail.smtp.auth", "true");
		   			mailProps.put("mail.smtp.host", host);
		   			Session mailSession = Session.getDefaultInstance(mailProps, auth);
		
		   			MimeMessage message = new MimeMessage(mailSession);
		
		   			message.setFrom(new InternetAddress(systemInfor.getWebEmail()));
		   			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
		   					systemInfor.getWebEmail()));
		
		   			message.setSubject("密码");
		   			MimeMultipart multi = new MimeMultipart();
		   			BodyPart textBodyPart = new MimeBodyPart();
		   			textBodyPart.setText("Email:" + user.getEmail() + "\n密码:" + user.getPassword());
		   			multi.addBodyPart(textBodyPart);
		   			message.setContent(multi);
		   			message.saveChanges();
		   			Transport.send(message);

        	    }catch(Exception ex){ 
        	    	ex.printStackTrace();
        	    	json.put("success", false);
        	    } 
        	    
		}catch(Exception e)
		{
			e.printStackTrace();
			json.put("success", false);
		}
		
		return json;
	}
	/**
	 * 删除用户所有信息
	 */
	public JSONObject findUserPassword(Integer id) {
		boolean isSuccess=userDao.findUserPassword(id);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 置顶
	 */
	public JSONObject top(int brandId, String flag) {
		boolean isSuccess=userDao.top(brandId,flag);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 获得列表
	 */
	public JSONObject menageCulmn(Pages pages, String title, String startTime,
			String endTime) {
		List list=userDao.menageCulmn(pages,title,startTime,endTime);
		JSONObject jsonObject=new JSONObject();
		JSONArray josnArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			
			BasicInfor userdetial=(BasicInfor)(list.get(i));
			JSONObject tempObject=new JSONObject();
			tempObject.put("category", userdetial.getCategory());
			tempObject.put("content", userdetial.getContent());
			tempObject.put("modify", "<a href='system.action?id="+userdetial.getId()+"&flag=0'  ><font color='blue'>修改</color></a>");
			tempObject.put("del", "<a href='javascript:void(0)' onclick='delculmn("+userdetial.getId()+");' ><font color='blue'>删除</color></a>");
			josnArray.add(tempObject);
		}
		jsonObject.put("total", pages.getRowCount());
		jsonObject.put("rows", josnArray);
		return jsonObject;
	}
	/**
	 * 删除栏目
	 */
	public JSONObject delculmn(int id) {
		boolean isSuccess=userDao.delculmn(id);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
		
	}
	/**
	 * 添加栏目
	 */
	public JSONObject addculmn(String title) {
		boolean isSuccess=userDao.addculmn(title);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 栏目列表
	 */
	public JSONObject culmnlist() {
		List list=userDao.culmnlist();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("success", list);
		return jsonObject;
	}
	public List culmnList() {
		List list=userDao.culmnlist();
		return list;
	}
	/**
	 * 获得分类用户列表
	 */
	public JSONObject findCategoryUserList() {
		List list=userDao.findCategoryUserList();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("success", list);
		return jsonObject;
	}
	/**
	 * 删除品牌所有话题
	 */
	public JSONObject delAllTopicForUser(int brandId) {
		boolean isSuccess=userDao.delAllTopicForUser(brandId);
		JSONObject json=new JSONObject();
		json.put("success", isSuccess);
		return json;
	}
	/**
	 * 获得关注我的人的列表数
	 */
	public int meToFriendList(int userId) {
		int count=userDao.meToFriendList(userId);
		return count;
	}
	/**
	 * 获得关注我的人的列表
	 */
	public List friendToMe(Pages page,int userId) {
		List list=userDao.friendToMe(page,userId);
		return list;
	}
	
}

class PopupAuthenticator extends Authenticator {
	String username = null; // 163邮箱登录帐号
	String pwd = null; // 登录密码

	PopupAuthenticator(String username, String pwd) {
		this.username = username;
		this.pwd = pwd;
	}

	public PasswordAuthentication getPasswordAuthentication() {

		return new PasswordAuthentication(username, pwd);
	}

}
